/* eslint-disable prettier/prettier */
import "./index.less";
import Table from "./components/table/index";
import { useState, useEffect } from "react";
import { getDictTypePage } from "@/api/system/dict/type";
import { getDictDataPage } from "@/api/system/dict/data";
import { TimeDate } from "@/utils/util";

const useDict = () => {
	const [leftData, setLeftData] = useState({});
	const [rightData, setRightDataData] = useState({});
	const getDictTypePageFn = (data: object) => {
		getDictTypePage(data).then(res => {
			res.list.forEach((item: any) => {
				item.createTime = TimeDate(item.createTime);
			});
			setLeftData(res);
		});
	};
	const getDictDataPageFn = (data: object) => {
		getDictDataPage(data).then(res => {
			res.list.forEach((item: any) => {
				item.createTime = TimeDate(item.createTime);
			});
			setRightDataData(res);
		});
	};
	const setTypeFn = (dictType: string) => {
		getDictDataPageFn({ pageNo: 1, pageSize: 10, dictType });
	};
	useEffect(() => {
		getDictTypePageFn({ pageNo: 1, pageSize: 10 });
		getDictDataPageFn({ pageNo: 1, pageSize: 10 });
	}, []);

	return (
		<div className="dict">
			<Table onSetType={setTypeFn} data={leftData} position={"left"} ongetDict={getDictTypePageFn}></Table>
			<Table data={rightData} position={"right"} ongetDict={getDictDataPageFn}></Table>
		</div>
	);
};

export default useDict;
